<template>
  <el-row  
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    >
    <el-col :span="10">
      <div class="block" v-loading="treeLoading">
        <h4>菜单管理</h4>
        <el-tree
          :data="treeData"
          node-key="id"
          default-expanded-keys="1"
          :expand-on-click-node="false">
          <span class="custom-tree-node" slot-scope="{ node, data }" @mouseenter="menuEnter($event, data)" @mouseleave="menuLeave($event, data)" @dblclick="dblclickFun(node, data)">
            <span>{{ node.label }}</span>
            <span class="menu-operates">
              <el-button
                type="text"
                size="mini"
                @click="() => append(data)">
                <i class="el-icon-circle-plus"></i>
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="() => update(node, data)">
                <i class="el-icon-edit"></i>
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="() => remove(node, data)">
                <i class="el-icon-remove"></i>
              </el-button>
            </span>
          </span>
        </el-tree>
      </div>
    </el-col>
    <el-col :span="14">
      <h4>菜单编辑</h4>
      <el-form ref="form" :model="form" label-width="80px" size="small">
        <el-form-item label="上级菜单" 
          prop="parentName"
          :rules="[
            { required: true, message: '上级菜单不能为空'}
          ]"
        >
          <el-input v-model="form.parentName"  :readonly="true" ></el-input>
          <input v-model="form.parentId"  type="hidden" />
        </el-form-item>
        <el-form-item label="菜单名称" 
          prop="name"
          :rules="[
            { required: true, message: '菜单名称不能为空'}
          ]"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单URL" :required="true"
          prop="url"
          :rules="[
            { required: true, message: '菜单URL不能为空'}
          ]"
        >
          <el-input v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径" 
          prop="path"
          :rules="[
            { required: true, message: '菜单路径不能为空'}
          ]"
        >
          <el-input v-model="form.path" :required="true"></el-input>
        </el-form-item>
        <el-form-item label="菜单组件"   
          prop="component"
          :rules="[
            { required: true, message: '菜单组件不能为空'}
          ]"
        >
          <el-input v-model="form.component"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标">
          <el-input v-model="form.iconCls" aria-placeholder="fa fa-bug"></el-input>
        </el-form-item>
        <el-form-item label="菜单排序">
          <el-input-number v-model="form.order" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>
export default {
  data() {
    return {
      treeData: [],
      treeLoading: false,
      loading: false,
      nowNodeData: [],
      form: {
        parentName: '',
        parentId: '',
        id: '',
        name: '',
        url: '',
        path: '',
        component: '',
        iconCls: '',
        order: ''
      }
    };
  },
  mounted: function() {
    this.treeLoading = true;
    this.loadTreeData();
  },
  methods: {
    loadTreeData() {
      var _this = this;
      this.getRequest("/system/menu/menuTree").then(resp => {
        _this.treeLoading = false;
        if (resp && resp.status == 200) {
          _this.treeData = resp.data;
        }
      });
    },
    append(data) {
      this.form.parentName = data.label;
      this.form.parentId = data.id;
      this.nowNodeData = data;
      this.form.id = '';
    },
    onSubmit() {
      var _this = this;
      this.$refs['form'].validate((valid) => {
          if (valid) {
            _this.loading = true;
            var param = {};
            var url;
            //新增
            if(_this.form.id == '') {
              param = {
                name: _this.form.name,
                url: _this.form.url,
                parentId: _this.form.parentId,
                path: _this.form.path,
                component: _this.form.component,
                iconCls: _this.form.iconCls,
                order: _this.form.order
              }
              url = "/system/menu/menuAdd";
            } else {
              param = {
                name: _this.form.name,
                url: _this.form.url,
                id: _this.form.id,
                path: _this.form.path,
                component: _this.form.component,
                iconCls: _this.form.iconCls,
                order: _this.form.order
              }
              url = "/system/menu/menuUpdate";
            }
            this.postRequest(url, param).then(resp=> {
              _this.loading = false;
              if (resp && resp.status == 200) {
                var respData = resp.data;
                _this.$message({type: respData.status, message: respData.msg});
                if(_this.form.id == '') {
                  //前端添加菜单
                  const newChild = { id: respData.result, label: _this.form.name, children: []};
                  if (!_this.nowNodeData.children) {
                    _this.$set(_this.nowNodeData, "children", []);
                  }
                  _this.nowNodeData.children.push(newChild);  
                } else {
                  //前端更新菜单
                  _this.nowNodeData.label = _this.form.name;
                }  
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
    },
    update(node, data) {
      var _this = this;
      this.nowNodeData = data;
      this.loading = true;
      this.getRequest("/system/menu/queryMenuById?id="+data.id).then(resp => {
        this.loading = false;
        if (resp && resp.status == 200) {
          var respData = resp.data;
          _this.form.parentName = node.parent.label;
          _this.form.parentId = node.parent.id;
          _this.form.name = respData.name;
          _this.form.id = respData.id;
          _this.form.url = respData.url || "/";
          _this.form.path = respData.path;
          _this.form.component = respData.component;
          _this.form.iconCls = respData.iconCls;
          _this.form.order = respData.order;
        }
      });
    },
    dblclickFun(node, data) {
      this.update(node, data);
    },
    remove(node, data) {
      var _this = this;
      this.$confirm('是否删除[' + data.label + ']菜单, 该菜单及下面所有子菜单将被删除!', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            _this.treeLoading = true;
            _this.deleteRequest("/system/menu/menuDelete/?menuId=" + data.id).then(resp=> {
              _this.treeLoading = false;
              if (resp && resp.status == 200) {
                var respData = resp.data;
                _this.$message({
                  message: respData.msg,
                  type: respData.status
                });
                const parent = node.parent;
                const children = parent.data.children || parent.data;
                const index = children.findIndex(d => d.id === data.id);
                children.splice(index, 1);
              }
            });
          }).catch(() => {
            _this.$message({
              type: 'info',
              message: '已取消删除'
            });
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    menuEnter: function(event, data) {
      if(data.id == 1) {
        //return;
      }
      var ct = event.target || event.srcElement;
      ct.className = ct.className + " custom-tree-node-display";
    },
    menuLeave: function(event, data) {
      if(data.id == 1) {
        //return;
      }
      var ct = event.target || event.srcElement;
      ct.className = "custom-tree-node";
    }
  }
};
</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.custom-tree-node .menu-operates {
  display: none;
  padding-left: 20px;
}
.custom-tree-node-display .menu-operates {
  display: block;
}
</style>
